﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DienLib.PopUp
{
    /// <summary>
    /// Interaction logic for PopUpOKCancel.xaml
    /// </summary>
    public partial class PopUpConfirmReplace : UserControl
    {
        PopUpResult _result = PopUpResult.NA;
        private PopUpConfirmReplace(System.IO.FileInfo fileE, System.IO.FileInfo fileN, string comment, string title)
        {
            InitializeComponent();
            if (title != null) tblTitle.Text = title;

            if (comment != null) tblComment.Text = comment;
            else comment = string.Format("The Destination already contains a file named \"IMG2489.JPG\"",fileN.Name);

            tblNameE.Text = fileE.Name;
            long bytes = fileE.Length;
            long KB = bytes/1024;
            tblSizeE.Text = string.Format("{0} KB ({1} bytes)", KB.ToString(), bytes.ToString("#,##0"));
            tblDateE.Text = fileE.LastWriteTime.ToString("G");

            tbNameN.Text = fileN.Name;
            bytes = fileN.Length;
            KB = bytes / 1024;
            tblSizeN.Text = string.Format("{0} KB ({1} bytes)", KB.ToString(), bytes.ToString("#,##0"));
            tblDateN.Text = fileN.LastWriteTime.ToString("G");

            //Run run = new Run(textBlock1.Text); run.Foreground = Brushes.Black; textBlock1.Inlines.Clear(); textBlock1.Inlines.Add(new Bold(run));
            //textBlock2.Inlines.Clear();
            //textBlock1.Inlines.AddRange(Command.HighlightTextBlockMatch(Title, "\"[^\"]*\"", null, true)); // "[^"]*" : Mean "*" any character with double-quotes
            //textBlock2.Inlines.AddRange(Command.HighlightTextBlockMatch(Comment, "\"[^\"]*\"", System.Windows.Media.Brushes.Blue, true));
        }

        /// <summary>
        /// Show Dialog for PopUpConfirmReplace
        /// </summary>
        /// <param name="fileE">Existing File</param>
        /// <param name="fileN">New File</param>
        /// <returns></returns>
        public static PopUpResult ShowDialog(System.IO.FileInfo fileE, System.IO.FileInfo fileN, string comment = null, string title = null)
        {
            PopUpConfirmReplace popup = new PopUpConfirmReplace(fileE, fileN, comment, title);
            Window window = new Window();
            window.Content = popup;
            window.SizeToContent = System.Windows.SizeToContent.WidthAndHeight;
            window.WindowStyle = System.Windows.WindowStyle.None;
            window.AllowsTransparency = true;
            window.Background = System.Windows.Media.Brushes.Transparent;
            window.Topmost = true;
            window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            window.MouseLeftButtonDown += Window_MouseLeftButtonDown;
            System.Media.SystemSounds.Beep.Play();
            window.ShowDialog();
            window.MouseLeftButtonDown -= Window_MouseLeftButtonDown;
            return popup._result;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            this._result = PopUpResult.OK;
            try { Window.GetWindow(this).Close(); }
            catch (Exception ex) { throw new InvalidOperationException("Cannot close Parent not Window : " + ex.Message); }
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            this._result = PopUpResult.CANCEL;
            try { Window.GetWindow(this).Close(); }
            catch (Exception ex) { throw new InvalidOperationException("Cannot close Parent not Window : " + ex.Message); }
        }

        private static void Window_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            (sender as System.Windows.Window).DragMove();
        }
    }
}
